<?php
/**
 * Simple debug wrapper
 * @package   nuo
 * @author    Nam Nguyen <nam.ngvan@gmail.com>
 * @version   1.0
 * @since     Apr 19, 2013
 */
class Nuo_Debug
{

	/**
	 * Get debug trace back
	 *
	 * @static
	 * @access public
	 * @return string
	 */
	static public function getTrace()
	{
		$trace = debug_backtrace();
		$output = '';

		foreach ($trace as $index => $stack)
		{
			// Process args
			$args = array();
			if (!empty($stack['args']))
			{
				foreach ($stack['args'] as $argIndex => $argValue)
				{
					if (is_object($argValue))
					{
						$args[$argIndex] = get_class($argValue);
					}
					else
					if (is_array($argValue))
					{
						$args[$argIndex] = 'Array';
						//substr(print_r($argValue, true), 0, 32);
					}
					else
					if (is_string($argValue))
					{
						$args[$argIndex] = "'" . substr($argValue, 0, 100) . (strlen($argValue) > 100 ? '...' : '') . "'";
					}
					else
					{
						$args[$argIndex] = print_r($argValue, true);
					}
				}
			}
			// Process message
			$output .= sprintf('#%1$d %2$s(%3$d): %4$s%5$s%6$s(%7$s)', $index, (!empty($stack['file']) ? $stack['file'] : '(unknown)'), (!empty($stack['line']) ? $stack['line'] : '(unknown)'), (!empty($stack['class']) ? $stack['class'] : ''), (!empty($stack['type']) ? $stack['type'] : ''), $stack['function'], join(', ', $args)) . PHP_EOL;
		}

		// Throw main in there for the hell of it
		$output .= sprintf('#%1$d {main}', $index + 1);

		return $output;
	}

}
